import request from '@/util/request';
import type { questionBanListType, QuestionListQueryParams, 
              ApiResponse , ChangeQuestion,  ExampleParams,GradingResult, 
              SearchData, Course, Knowledge,
              ExamQuestionBan, } from '@/types/QuestionBankType';
import { PageResult } from '@/components/Pagination.vue';

export const QuestionBankApi = {
  /**
   * 获取题库列表
   * @param params 查询参数
   * @returns 题目列表
   */
  getQuestionBankList: async (params: QuestionListQueryParams): Promise<ApiResponse<PageResult<questionBanListType>>> => {
    try {
      const response = await request.get<ApiResponse<PageResult<questionBanListType>>>("/questionban/list", { params });
      console.log(response.data);
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  /**
   * 获取具体题的信息
   * @param params 
   * @returns 
   */
  getQuestionBankListById: async (params: Partial<ChangeQuestion>): Promise<ApiResponse<ChangeQuestion>> => {
    {
      try {
        const response = await request.get<ApiResponse<ChangeQuestion>>("/questionban/id", { params });
        return response.data;
      } catch (error) {
        throw error;
      }
    }
  },
  /**
   * ai判题
   * @param params 
   * @returns 
   */
  checkAnswers: async (params: ExampleParams): Promise<GradingResult> => {
    try {
      const requestBody = {
        userId: params.userId,
        exercisesId: params.exercisesId,
        userAnswer: params.userAnswer
      };
      const response = await request.post<ApiResponse<GradingResult>>("/questionban/testQuestion", requestBody);
      console.log(response.data.code);
      console.log(response.data.data);
      return response.data.data;
    } catch (error) {
      console.error('批改题目失败:', error);
      throw error;
    }
  },


  /**
   * 获取课程列表
   * @returns 课程列表
   */
  getCourses: async (): Promise<Course[]> => {
    const res = await request.get<ApiResponse<Course[]>>('/questionban/courses');
    // console.log(res.data.data);
    return res.data.data;
  },

  /**
   * 获取知识点列表
   * @param courseId 课程ID
   * @returns 知识点列表
   */
  getKnowledgePoints: async (): Promise<Knowledge[]> => {
    const res = await request.get<ApiResponse<Knowledge[]>>('/knowledge/courseId');
    // console.log(res.data.data);
    return res.data.data;
  },

  /**
   * 搜索题库
   * @param params 
   * @returns 
   */
  searchQuestions: async (params: Partial<SearchData>): Promise<PageResult<SearchData>> => {
    try {
      console.log(params);
      const response = await request.get<ApiResponse<PageResult<SearchData>>>("/questionban/search",{params});
      // console.log(response.data);
      return response.data.data;
    } catch (error) {
      throw error;
    }
  },
  
  /**
   * 根据题目ID获取题目
   * @param exercisesIds 题目ID数组
   * @returns 题目列表
   */
  findQuestionsByExercisesId: async (exercisesIds: number[]): Promise<ApiResponse<ExamQuestionBan[]>> => {
    try {
      const response = await request.get<ApiResponse<ExamQuestionBan[]>>("/questionban/exam",
        {
          params: 
            { exercisesIds: exercisesIds.join(',') }
        });
      console.log(response.data);
      return response.data;
    } catch (error) {
      console.error('获取题目失败:', error);
      throw new Error(error.response.data.message || '获取题目失败');
      // throw error;
    }
  },
};